import com.lagou.dao.IUserDao;
import com.lagou.dao.impl.UserDaoImpl;
import com.lagou.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class test {

    @Test
    public void testMybatis() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        // 默认开启一个事务，但是该事务不会自动提交
        SqlSession sqlSession = build.openSession();
        // 在执行增删改任务操作时，要手动提交事务
        List<User> objects = sqlSession.selectList("com.lagou.dao.IUserDao.findAll");
        for (User object : objects) {
            System.out.println(object);
        }
    }
    /**
     * @description: 传统开发方式
     * @author oyz
     * @param
     * @date 2022/10/23 8:48
     * @version 1.0
     */
    @Test
    public void testMybatis2() throws IOException {
        IUserDao userDao = new UserDaoImpl();
        System.out.println(userDao.findAll());
    }

    /**
     * @description: 代理开发方式
     * @author oyz
     * @param
     * @date 2022/10/23 8:47
     * @version 1.0
     */
    @Test
    public void testMybatis3() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        // 默认开启一个事务，但是该事务不会自动提交
        SqlSession sqlSession = build.openSession(true);
        IUserDao dao = sqlSession.getMapper(IUserDao.class);
        System.out.println(dao.findAll());
    }
}
